package com.huawei.conf;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author 黄后林
 * @date 2022-11-05 11:31:14
 * @note 程序员是用代码喂出来的
 */
public class MybatisConfiguration {

    /**
     * 方法描述: 告诉Spring 去哪里加载 mybatis的对数据库的CRUD的SQL
     * @author: 黄后林
     * @date: 2022/10/19 17:02
     * @param dataSource
     * @return org.mybatis.spring.SqlSessionFactoryBean
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception{
        //xml文件所在路径
        SqlSessionFactoryBean sqlSessionFactoryBean= new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:dao/*Dao.xml");
        sqlSessionFactoryBean.setMapperLocations(resources);
        return  sqlSessionFactoryBean;
    }


    /**
     * 方法描述: mybatis中都是接口 告诉哪些接口的实现类要去xml文件中去找实现类
     * 凡是 com.xme.bookms 包及其子包下面的所有的接口
     * @author: 黄后林
     * @date: 2022/10/19 17:03
     * @param
     * @return org.mybatis.spring.mapper.MapperScannerConfigurer
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{
        //dao接口所在地包
        MapperScannerConfigurer mapperScannerConfigurer= new MapperScannerConfigurer();

        // com.huawei.billms.billtype.dao
        // com.huawei.billms.bills.dao
        mapperScannerConfigurer.setBasePackage("com.huawei.billms.*.dao");
        return mapperScannerConfigurer;
    }
}
